package com.studio49.exmple.controller.appInterface.appUser;

import java.util.Date;

import com.studio49.exmple.common.util.PubDateUtils;
import com.studio49.exmple.common.util.ValidateUtils;
import com.studio49.exmple.core.controller.AppBaseController;
import com.studio49.exmple.core.entity.pub.JsonResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.studio49.exmple.entity.appInterface.appUser.LoginReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.MobileUpdateReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.OpenidLoginReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.RegisterReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.UserNameReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.UuidLoginReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.VerificationCodeLoginReqEntity;
import com.studio49.exmple.entity.appInterface.appUser.VerificationCodeReqEntity;
import com.studio49.exmple.service.appInterface.AppUserService;

/**
 * 
 * @author chensj
 * @date 2019年2月16日 上午11:50:57
 * @version 1.0
 * 
 * 错误码范围{1001~1999}
 */
@Controller
@RequestMapping("/appInterface/appUser")
public class AppUserController extends AppBaseController {
	
	private static final Logger LOG = LogManager.getLogger(AppUserController.class.getName());
	
	@Autowired
	private AppUserService appUserService;
	
	/**
	 * 获取验证码
	 * 
	 * @param verificationCodeReqEntity
	 * @return
	 */
	@RequestMapping(value = "/getVerificationCode", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getVerificationCode(VerificationCodeReqEntity verificationCodeReqEntity) {
		LOG.warn("getVerificationCode start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(verificationCodeReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(verificationCodeReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.getVerificationCode(verificationCodeReqEntity);
		} catch (RuntimeException e) {
			super.setErrMsg(e.getMessage(), LOG, jsonResult, e);
		} catch (Exception e1) {
			super.setErrMsg("验证码获取异常!", LOG, jsonResult, e1);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("getVerificationCode end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	/**
	 * 手机注册
	 * 
	 * @param registerReqEntity
	 * @return
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult register(RegisterReqEntity registerReqEntity) {
		LOG.warn("register start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(registerReqEntity));
		JsonResult jsonResult = new JsonResult();
		// 1.验证请求参数
		try {
			ValidateUtils.check(registerReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.register(registerReqEntity);
		} catch (Exception e) {
			super.setErrMsg("注册异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("register end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	/**
	 * 手机登录接口
	 * 
	 * @param loginReqEntity
	 * @return
	 */
	@RequestMapping(value = "/mobileLogin", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult mobileLogin(LoginReqEntity loginReqEntity) {
		LOG.warn("mobileLogin start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(loginReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(loginReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.mobileLogin(loginReqEntity);
		} catch (Exception e) {
			super.setErrMsg("登录异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("mobileLogin end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	/**
	 * 手机，验证码登录
	 * 
	 * @param reqEntity
	 * @return
	 */
	@RequestMapping(value = "/verificationCodeLogin", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult verificationCodeLogin(VerificationCodeLoginReqEntity verificationCodeLoginReqEntity) {
		LOG.warn("verificationCodeLogin start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(""));
		JsonResult jsonResult = new JsonResult(true);
		try {
			jsonResult = appUserService.verificationCodeLogin(verificationCodeLoginReqEntity);
		} catch (Exception e) {
			super.setErrMsg("服务器异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("verificationCodeLogin end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	
	/**
	 * uuid登录
	 * 
	 * @param wechatLoginReqEntity
	 * @return
	 */
	@RequestMapping(value = "/uuidLogin", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult uuidLogin(UuidLoginReqEntity uuidLoginReqEntity) {
		LOG.warn("uuidLogin start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(uuidLoginReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(uuidLoginReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.uuidLogin(uuidLoginReqEntity);
		} catch (Exception e) {
			super.setErrMsg("uuid登录异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("uuidLogin end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	
	/**
	 * uuid登录
	 * 
	 * @param wechatLoginReqEntity
	 * @return
	 */
	@RequestMapping(value = "/openidLogin", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult openidLogin(OpenidLoginReqEntity openidLoginReqEntity) {
		LOG.warn("openidLogin start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(openidLoginReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(openidLoginReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.openidLogin(openidLoginReqEntity);
		} catch (Exception e) {
			super.setErrMsg("openid登录异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("openidLogin end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	
	/**
	 * 修改用户名
	 * 
	 * @param wechatLoginReqEntity
	 * @return
	 */
	@RequestMapping(value = "/modifyUserName", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult modifyUserName(UserNameReqEntity userNameReqEntity) {
		LOG.warn("modifyUserName start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(userNameReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(userNameReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.modifyUserName(userNameReqEntity);
		} catch (Exception e) {
			super.setErrMsg("修改用户名时异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("modifyUserName end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}
	
	/**
	 * 绑定手机号码
	 * 
	 * @param wechatLoginReqEntity
	 * @return
	 */
	@RequestMapping(value = "/updateMobile", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateMobile(MobileUpdateReqEntity mobileUpdateReqEntity) {
		LOG.warn("updateMobile start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(mobileUpdateReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(mobileUpdateReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		// 2.业务处理
		try {
			jsonResult = appUserService.updateMobile(mobileUpdateReqEntity);
		} catch (Exception e) {
			super.setErrMsg("修改用户名时异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("updateMobile end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}

	/**
	 * APP用户重置密码
	 *
	 * @param registerReqEntity
	 * @return
	 */
	@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult resetPassword(RegisterReqEntity registerReqEntity) {
		LOG.warn("resetPassword start ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		LOG.warn("reqData: " + JSON.toJSONString(registerReqEntity));
		JsonResult jsonResult = new JsonResult(true);
		// 1.验证请求参数
		try {
			ValidateUtils.check(registerReqEntity);
		} catch (RuntimeException e1) {
			jsonResult = super.setCheckErrMsg(LOG, jsonResult, e1);
			return jsonResult;
		}
		try {
			jsonResult = appUserService.resetPassword(registerReqEntity);
		} catch (Exception e) {
			super.setErrMsg("修改密码异常!", LOG, jsonResult, e);
		}
		LOG.warn("resData: " + JSON.toJSONString(jsonResult));
		LOG.warn("resetPassword end ... " + PubDateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss:SSS"));
		return jsonResult;
	}

}
